#include "widget_buy.h"
#include "units.h"
#include "players.h"



buy_widget::buy_widget( QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	connect( this, SIGNAL( send_command( player*, const string& ) ), game::get_instance(), SLOT( command_received( player*, const string& ) ) );

	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );
	
	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Players settings</font></b>" );
	title_w_l->addWidget( title_l );

	next_b = new QPushButton( "NEXT", title_w );
	next_b->setFixedWidth( 150 );
	title_w_l->addWidget( next_b );
	connect( next_b, SIGNAL( clicked() ), this, SLOT( next() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	layout->addSpacing( 20 );


	current_widget = new QWidget();
	layout->addWidget( current_widget );

	this->setLayout( layout );
};



QWidget* buy_widget::create_players_widget()
{
	player* current_player =  game::get_instance()->get_current_player();
	switch ( current_player->get_type() )
	{
	case ai:
		ai_w = new ai_widget( current_player, next_b );
		return ai_w;
		break;
	case network:
		network_w = new network_widget( current_player, next_b );
		return network_w;
		break;
	case local:
		human_w = new human_widget( current_player, next_b );
		return human_w;
		break;
	default:
		//error - unknown type
		return new QWidget();
		break;
	}
};



void buy_widget::show_next_turn()
{
	current_widget->deleteLater();
	current_widget = create_players_widget();
	layout->addWidget( current_widget );
};



void buy_widget::next()
{
	emit send_command( game::get_instance()->get_current_player(), string( "END" ) );
};